package com.syh.bishe.service.impl;

import com.syh.bishe.pojo.Ask;
import com.syh.bishe.mapper.AskMapper;
import com.syh.bishe.service.IAskService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author syh
 * @since 2024-07-10
 */
@Service
@RequiredArgsConstructor
public class AskServiceImpl extends ServiceImpl<AskMapper, Ask> implements IAskService {
    private final AskMapper askMapper;
    @Override
    @Transactional
    public void add_ask(String question, String res, Integer id, Integer userid) {
        Ask ask = new Ask();
        ask.setQuestion(question)
                .setAnswer(res)
                .setCompanyId(id)
                .setUserId(userid)
                .setDate(LocalDate.now());
        askMapper.insert(ask);
    }

    @Override
    public List<String> getAsk(Integer id, Integer userid) {
        List<Ask> asks = askMapper.select_ask(id, userid, LocalDate.now());
        List<String> res = new ArrayList<>();
        for (Ask ask : asks) {
            res.add(ask.getQuestion());
            res.add(ask.getAnswer());
        }
        return res;
    }
}
